package com.vito.framework.system.config.controller;


import com.alibaba.fastjson.JSONObject;
import com.vito.framework.base.controller.BaseController;
import com.vito.framework.base.exception.CommonException;
import com.vito.framework.system.config.model.SysConfig;
import com.vito.framework.system.config.service.SysConfigService;
import com.vito.support.ApiResponse;
import com.vito.support.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/***************************************************************
 * Name:        SysConfigController.java
 * Create At:   2018年3月11日
 * Module:      
 * Description: [基础业务-系统配置]
 * @author wanglina
 * ------------------------------------------------------------
 * 修改历史
 * 序号         日期                       修改人               修改原因
 * 1    2018年3月11日      wanglina    
 * 2
 ***************************************************************/
@RestController
@RequestMapping("${adminPath}/sysConfig")
public class SysConfigController extends BaseController {

    @Autowired
    public SysConfigService sysConfigService;

    /**
     * Create At:   2018年3月11日
     *
     * @param
     * @return
     * @author wanglina
     * @description[查询系统配置的信息]
     */
    @RequestMapping(value = "getList", consumes = "application/json;charset=UTF-8")
    public ApiResponse getList() {
        Map<String, Object> resMap = new HashMap<>();
        try {
            SysConfig sysConfig = sysConfigService.get();
            if (sysConfig == null) {
                resMap.put("sysConfig", new SysConfig());
            } else {
                resMap.put("sysConfig", sysConfig);
                resMap.put("method", "edit");
            }
            return ApiResponse.ok(ResultEnum.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.of(e);
        }
    }

    /**
     * Create At:   2018年3月11日
     *
     * @param
     * @return
     * @author wanglina
     * @description[保存添加的信息]
     */
    @RequestMapping(value = "addSave", consumes = "application/json;charset=UTF-8")
    public ApiResponse addSave(HttpServletRequest request, @RequestBody JSONObject params) {
        Map<String, Object> resMap = new HashMap<>();

        SysConfig sysConfig = JSONObject.parseObject(params.getJSONObject("sysConfig").toJSONString(), SysConfig.class);

        try {
            sysConfigService.insert(sysConfig);
            resMap.put("message", "添加成功");

            return ApiResponse.ok(ResultEnum.OK);
        } catch (CommonException e) {
            e.printStackTrace();
            return ApiResponse.of(e);
        }
    }

    /**
     * Create At:   2018年3月11日
     *
     * @param
     * @return
     * @author wanglina
     * @description[保存修改的信息]
     */
    @RequestMapping(value = "editSave")
    public ApiResponse editSave(HttpServletRequest request, @RequestBody JSONObject params) {
        try {
            SysConfig sysConfig = JSONObject.parseObject(params.getJSONObject("sysConfig").toJSONString(), SysConfig.class);
            sysConfigService.update(sysConfig);

            return ApiResponse.ok(ResultEnum.OK);
        } catch (CommonException e) {
            e.printStackTrace();
            return ApiResponse.of(e);
        }
    }

}
